package com.nari.ysphd.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.nari.ysphd.R;
import com.jeremyfeinstein.gson.Location;
import com.jeremyfeinstein.utils.YCHSInstance;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2018/2/2.
 */

public class LocationAdapter extends NewBaseAdapter {
    List<Location> locations ;
    private Context mContext;
    public LocationAdapter(Context context) {
        mContext = context;
        locations = YCHSInstance.getInstance().getLocations();
    }


    @Override
    public int getCount() {
        return locations == null ? 0 : locations.size();
    }

    @Override
    public Object getItem(int position) {
        return locations.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_select,
                    null);
        }
        TextView locationName = (TextView) convertView
                .findViewById(R.id.item_name2);
        Location selectItem = (Location) getItem(position);
        locationName.setText(selectItem.getRESOURCE_NAME());
        return convertView;
    }
    public void refreshList(String text) {
        List<Location> tempLocations = new ArrayList<Location>();
        if (text != null && !text.isEmpty()) {
            for (Location item : locations) {
                if (item.getRESOURCE_NAME().contains(text)) {
                    tempLocations.add(item);
                }
            }
            locations = tempLocations;
        }
        notifyDataSetChanged();
    }

}